import React, { Component } from 'react';
import { actions } from 'mirrorx';
import { Dropdown, Menu, Button, Message,Upload } from 'tinper-bee';
import {searchField} from '../InitTemplate';
import { Warning,handleSearchValue } from 'utils';
import './index.less';

const { Item } = Menu;
class AcExport extends Component {
    constructor(props) {
        super(props);
        this.state = {  
            selIndex: "0",
        };

    }

    // 下拉选择函数
    onSelect = ({ key }) => {
        
        this.setState({
            selIndex : key
        })

        this.exportExcel(key);
    }

    exportExcel = (selIndex) => {
        let  { pageIndex, pageSize, total, selectData, list,queryParam } = this.props;
        let param;
        let name = "预算方案";
        if ( selIndex == '1' ) {
            if(selectData == 0) {
                Message.create({ content: '请先选择数据', color: 'warning', duration: 10 });
                return;
            }
            let ids = selectData.map((n) => n.id).join();
            param = {
                GET_TOEXPORTEXCEL:`/occ-fee/fee/budget-plan-excel/excelMergeDataExport`,
                dataList:{
                    ids:ids,
                    isEdit: false,
                    isAll: false,
                    isPdf:false,
                    excelFileName: name

                }
            }
            actions.RetailOrders.exportExcel(param);
        } else if(selIndex == '2'){
            let searchvalue = handleSearchValue(this.props.queryParam, searchField);
            delete searchvalue.size;
            delete searchvalue.page;
            
            
            param = {
                GET_TOEXPORTEXCEL:`/occ-fee/fee/budget-plan-excel/excelMergeDataExport`,
                dataList:{
                    searchParams: searchvalue,
                    isEdit: false,
                    isAll: true,
                    isPdf:false,
                    excelFileName: name
                }
            }
            actions.RetailOrders.exportExcel(param);
        }

    }

    isEmpty = (data) => {
        if ( !data || (data && data.length === 0) ) {
            Warning('请选择数据');
            return true;
        }
        return false;
    }

    onFileChange =(info) => {
        if (info.file.status !== 'uploading') {
        }
        if (info.file.status === 'done') {
            let res = info.file.response;
            if (res.status) {
                Message.create({ content: res.msg, color: 'warning', duration: 10 });
                return;
            }
        } else if (info.file.status === 'error') {
            
        }
    }

    render() {
        let self = this;
        let { className,authCodeList} = self.props;
        //文件上传
        const fileUpload = {
            name: 'file',
            action: `/occ-fee/fee/budget-plan-excel/mergeExcelDataImport`,     //上传地址
            data: { isEdit: "false"},
            headers: {
            },
            accept: ".xlsx,.xls",
            showUploadList: false,
            beforeUpload: this.beforeUpload,
            onChange: this.onFileChange
        };
        // //文件上传
        // const fileUploadedit = {
        //     name: 'file',
        //     action: `/occ-fee/fee/budget-plans-excel/mergeBudgetPlan-ExcelDataImport`,     //上传地址
        //     data: { isEdit: "true"},
        //     headers: {
        //     },
        //     accept: ".xlsx,.xls",
        //     showUploadList: false,
        //     beforeUpload: this.beforeUpload,
        //     onChange: this.onFileChange
        // };


        const menu1 = (
            <Menu
                selectedKeys = {[]}
                onSelect={self.onSelect}>
                <Item key="1">导出选中数据(新增)</Item>
                <Item key="2">符合当前查询条件的全部数据行(新增)</Item>
            </Menu>
        );

        const menu2 = (
            <Menu>
                <Item key="0">
                    <Upload {...fileUpload}>
                        <span>导入新增数据</span>
                    </Upload>
                </Item>
            </Menu>
        );

        
        return (
            <div className="AcExport">
               {authCodeList.indexOf("BudgetPlanimport")!==-1 &&
                <Dropdown 
                   overlay={menu2}
                   animation="slide-up"
                 >
                    <Button shape='border' style={{"marginLeft":15}} className={className} size='sm' >
                        导入
                    </Button>
                </Dropdown>
                }
                
                {authCodeList.indexOf("BudgetPlanexport")!==-1 &&  
                <Dropdown
                   trigger={['click']}
                   overlay={menu1}
                   animation="slide-up"
                 >
                    <Button shape='border' style={{"marginLeft":15}} className={className} size='sm' >
                        导出
                    </Button>
                 </Dropdown>
               }

            </div>
        );
    }
}

export default AcExport;